package com.rich.dmr.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.rich.dmr.R;
import java.util.List;
import rich.dmr.DmrManager;

public class DialingGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private DialingGridAdapter.OnItemClickListener mListener;
    private List<String> list;
    private DmrManager dmrManager;
    private int contact;
    private LayoutInflater mLayoutInflater;
    @SuppressLint("WrongConstant")
    public DialingGridAdapter(Context mContext, List<String> list) {
        this.mContext = mContext;
        this.list = list;
        dmrManager = (DmrManager)mContext.getSystemService("dmrservice");
        contact = dmrManager.getTransmitContact();
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    public enum ITEM_TYPE {
        ITEM_TYPE_BUTTON,
        ITEM_TYPE_TEXT
    }


    public interface OnItemClickListener{
        void ItemClickListener(View view, int position);
        void ItemLongClickListener(View view,int position);
    }

    public void setOnClickListener(DialingGridAdapter.OnItemClickListener listener){
        this.mListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 10) {
            return ITEM_TYPE.ITEM_TYPE_BUTTON.ordinal();
        } else {
            return ITEM_TYPE.ITEM_TYPE_TEXT.ordinal();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.ITEM_TYPE_BUTTON.ordinal()) {
            return new RelativeViewHolder(mLayoutInflater.inflate(R.layout.item_rv_call_btn, parent, false));
        } else {
            return new TextViewHolder(mLayoutInflater.inflate(R.layout.item_rv_call_num, parent, false));
        }
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {


        if (holder instanceof TextViewHolder) {
            if(position < list.size()){
                ((TextViewHolder)holder).callNum.setText(list.get(position));
            }
        } else if (holder instanceof RelativeViewHolder) {

        }


        if(mListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mListener.ItemClickListener(holder.itemView,pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mListener.ItemLongClickListener(holder.itemView,pos);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    class TextViewHolder extends RecyclerView.ViewHolder {
        private TextView callNum;

        public TextViewHolder(View itemView) {
            super(itemView);
            callNum = itemView.findViewById(R.id.tv_call_num);
        }
    }

    public static class RelativeViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout callBtn;
        RelativeViewHolder(View itemView) {
            super(itemView);
            callBtn = itemView.findViewById(R.id.rl_call);
        }
    }

}
